﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using UIControls.Core;
using System.Windows.Forms.Design;
using BO;
namespace UIControls
{
    public class DateRangeCell : Core.LabelCell, IUIControl
    {
        private CsDateTimePicker dateBegin;
        private CsDateTimePicker dateEnd;
        private CsComboBox comBox;
        public DateRangeCell()
        {
            InitializeComponent();

        }

        private DateRange _Range = DateRange.Today;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [DefaultValue(DateRange.Today)]
        [Browsable(false)]
        public DateRange Range
        {
            get{return _Range;}
            set
            {
                if (_Range != value&&value!= DateRange.Custom)
                {
                    _Range = value;

                    comBox.ComboBox.SelectedValue = value;
                }
            }
        }
        private void InitializeComponent()
        {
            int boxWidth = 90;
            SuspendLayout();
            dateBegin = new CsDateTimePicker();
            
            dateBegin.LabelWidth = 5;
            dateBegin.Width = boxWidth +5;
            dateEnd = new CsDateTimePicker();
            dateEnd.LabelText = "- ";
            dateEnd.LabelWidth = 15;
            dateEnd.Width = boxWidth + 15;
            comBox = new CsComboBox();
            comBox.LabelText = "";
            comBox.LabelWidth = 0;
            comBox.Width = boxWidth;
            this.Controls.Add(dateBegin);
            this.Controls.Add(dateEnd);
            this.Controls.Add(comBox);
            dateBegin.DateTimePicker.Value = DateTime.Today;
            dateEnd.DateTimePicker.Value = DateTime.Today;
            comBox.BindTo(this, "Range");

            comBox.ComboBox.SelectedValueChanged += ComboBox_SelectedValueChanged;
            dateBegin.DateTimePicker.ValueChanged += DateTimePicker_ValueChanged;
            dateEnd.DateTimePicker.ValueChanged += DateTimePicker_ValueChanged;

            base.Text = "日期范围";
            ResumeLayout();
        }
        public event EventHandler DateRangeChanged;
        void DateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            comBox.ComboBox.SelectedValue = DateRange.Custom;
        }

        void ComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            dateBegin.DateTimePicker.ValueChanged -= DateTimePicker_ValueChanged;
            dateEnd.DateTimePicker.ValueChanged -= DateTimePicker_ValueChanged;
            var r = (DateRange)comBox.ComboBox.SelectedValue;
            var w = (int)DateTime.Today.DayOfWeek;
            var td = DateTime.Today;
            switch (r)
            {
                case DateRange.Today:
                    BeginDate = td;
                    EndDate = td;
                    break;
                case DateRange.Yesterday:
                    BeginDate = td.AddDays(-1);
                    EndDate = BeginDate;
                    break;
                case DateRange.ThisWeek:

                    BeginDate = td.AddDays(w -7);
                    EndDate = BeginDate.AddDays(6);
                    break;
                case DateRange.LastWeek:
                    BeginDate = td.AddDays(w - 14);
                    EndDate = BeginDate.AddDays(6);
                    break;
                case DateRange.ThisMonth:
                    BeginDate = new DateTime(td.Year, td.Month, 1);
                    EndDate = BeginDate.AddMonths(1).AddDays(-1);
                    break;
                case DateRange.LastMonth:
                    BeginDate = new DateTime(td.Year, td.Month - 1, 1);
                    EndDate = BeginDate.AddMonths(1).AddDays(-1);
                    break;
                case DateRange.ThisYear:
                    BeginDate = new DateTime(td.Year, 1, 1);
                    EndDate = new DateTime(td.Year, 12, 31);
                    break;
                case DateRange.LastYear:
                    BeginDate = new DateTime(td.Year-1, 1, 1);
                    EndDate = new DateTime(td.Year-1, 12, 31);
                    break;
                case DateRange.All:
                    BeginDate = new DateTime(1949, 1, 1);
                    EndDate = new DateTime(9000, 1, 1);
                    break;
                case DateRange.Last7Days:
                    BeginDate = td.AddDays(-7);
                    EndDate = td;
                    break;
                case DateRange.Custom:
                    break;

            }
            if (DateRangeChanged != null)
            {
                DateRangeChanged(null, null);
            }
            dateBegin.DateTimePicker.ValueChanged += DateTimePicker_ValueChanged;
            dateEnd.DateTimePicker.ValueChanged += DateTimePicker_ValueChanged;
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public DateTime BeginDate
        {
            get { return dateBegin.DateTimePicker.Value; }
            set { dateBegin.DateTimePicker.Value = value; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public DateTime EndDate
        {
            get { return dateEnd.DateTimePicker.Value; }
            set { dateEnd.DateTimePicker.Value = value; }
        }
        private int _LabelWidth = 70;
        public int LabelWidth
        {
            get { return _LabelWidth; }
            set
            {
                _LabelWidth = value;
            }
        }
        [DefaultValue("日期范围")]
        public string LabelText
        {
            get { return base.Text; }
            set { base.Text = value; }
        }


        protected override Size DefaultSize
        {
            get
            {
                return new Size(380, 30);
            }
        }
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, DefaultSize.Height, specified);
        }


        protected override void OnLayout(LayoutEventArgs levent)
        {
            this.dateBegin.Location = new Point(LabelWidth + 10 + this.comBox.Width, 0);
            this.dateEnd.Location = new Point(LabelWidth + 10 + this.comBox.Width+dateBegin.Width + 5, 0);
            this.comBox.Location = new Point(LabelWidth+10, 0);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawString(this.LabelText, this.Font, new SolidBrush(GlobalUIVar.gLabelColor), new Point(e.ClipRectangle.Left + 5,  e.ClipRectangle.Top + 10));


        }


    }



}
